/*
 * @Descripttion:
 * @version:
 * @Author: ChenXiang
 * @Date: 2021-03-22 16:18:10
 * @LastEditors: ChenXiang
 * @LastEditTime: 2021-09-30 15:23:24
 */
import _extends from "babel-runtime/helpers/extends";


export default {
  omit: (obj, fields) => {
    var shallowCopy = _extends({}, obj);
    for (var i = 0; i < fields.length; i++) {
      var key = fields[i];
      delete shallowCopy[key];
    }
    return shallowCopy;
  },
  isObject: (obj: object): boolean => typeof obj === "object" && obj != null,

  // 从对象检索给定选择器指示的一组属性？

  // get: (from, ...selectors) =>
  //   [...selectors].map((s) =>
  //     s
  //       .replace(/[([^[]]*)]/g, ".$1.")
  //       .split(".")
  //       .filter((t) => t !== "")
  //       .reduce((prev, cur) => prev && prev[cur], from)
  //   ),
  // const obj = { selector: { to: { val: "val to select" } }, target: [1, 2, { a: "test" }] };
  // Example
  // get(obj, "selector.to.val", "target[0]", "target[2].a"); // ["val to select", 1, "test"]

  deepClone: function <T extends {}>(source: T, hash = new WeakMap()): T {
    if (!this.isObject(source)) return source; // 非对象返回自身

    if (hash.has(source)) return hash.get(source); // 查询hash表

    const target: any = Array.isArray(source) ? [] : {};

    hash.set(source, target); // 新增代码，哈希表设值

    for (const key in source) {
      // Object.prototype.hasOwnProperty.call 为了防止x.hasOwnProperty的出现
      if (Object.prototype.hasOwnProperty.call(source, key)) {
        if (typeof source[key] === "object") {
          target[key] = this.deepClone(source[key], hash); // 注意这里
        } else {
          target[key] = source[key];
        }
      }
    }
    return target;
  },
  isArrayEqual: (a, b) => {

  },
  // 判断两个对象是否键值相同
  isObjectEqual: (a, b) => {
    const aProps = Object.getOwnPropertyNames(a);
    const bProps = Object.getOwnPropertyNames(b);

    if (aProps.length !== bProps.length) {
      return false;
    }

    for (let i = 0; i < aProps.length; i++) {
      const propName = aProps[i];

      if (a[propName] !== b[propName]) {
        return false;
      }
    }
    return true;
  }
};
